package com.itheima.web.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.exception.CustomException;
import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.sms.FlashPromotionProductRelationService;
import com.itheima.utils.StringUtils;
import com.itheima.vo.pms.Product;
import com.itheima.vo.sms.SmsCouponHistory;
import com.itheima.vo.sms.SmsFlashPromotionProductRelation;
import com.itheima.web.mapper.sms.FlashPromotionProductRelationMapper;
import com.itheima.web.service.impl.pms.PmsProductServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class FlashPromotionProductRelationServiceImpl extends ServiceImpl<FlashPromotionProductRelationMapper, SmsFlashPromotionProductRelation> implements FlashPromotionProductRelationService {

    @Autowired
    private PmsProductServiceImpl  pmsProductService;
    /**
     * 批量选择商品添加关联
     *
     * @param relations
     * @return
     */
    @Override
    public ResultData
    create(List<SmsFlashPromotionProductRelation> relations) {
        if (relations == null) {
            throw new CustomException("暂无数据！");
        }
        if (relations.size() != 0) {
            for (SmsFlashPromotionProductRelation flashPromotionProductRelation : relations) {
                if (flashPromotionProductRelation != null) {
                    this.save(flashPromotionProductRelation);
                }
            }
            return ResultData.ok("操作成功", null);
        } else {
            return ResultData.error("暂无数据");
        }

    }

    /**
     * 删除关联
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) {
        boolean result = this.removeById(id);

        return result ? 1 : 0;
    }

    /**
     * 分页查询不同场次关联及商品信息
     *
     * @param pageNum
     * @param pageSize
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @return
     */
    @Override
    public PageResult seleteAll(Long pageNum, Long pageSize, Long flashPromotionId, Long flashPromotionSessionId) {
        //构造分页构造器
        Page<SmsFlashPromotionProductRelation> pageInfo = new Page<>(pageNum, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(flashPromotionId != null, SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        lqw.eq(flashPromotionSessionId != null, SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId);
        lqw.orderByAsc(SmsFlashPromotionProductRelation::getSort);
        lqw.orderByDesc(SmsFlashPromotionProductRelation::getId);
        this.page(pageInfo,lqw);
        List<SmsFlashPromotionProductRelation> records = pageInfo.getRecords();
        for (int i = 0; i < records.size(); i++) {
            SmsFlashPromotionProductRelation relation = records.get(i);
                Long productId = relation.getProductId();
                if (productId!=null){
                    Product product = pmsProductService.getById(productId);
                    relation.setProduct(product);
                }else {
                    records.remove(i);
                }
        }
        return PageResult.init(pageInfo.getTotal(), pageSize, pageNum, records);

    }

    /**
     * 修改关联信息
     *
     * @param smsFlashPromotionProductRelation
     * @return
     */
    @Override
    public int updateRelation(Long id, SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        //条件构造器
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, SmsFlashPromotionProductRelation::getId, id);
        boolean result = this.update(smsFlashPromotionProductRelation, lqw);
        return result ? 1 : 0;
    }

    /**
     * 获取管理商品促销信息
     *
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotionProductRelation get(Long id) {
        return this.getById(id);
    }
}
